package org.millioncall.yueyoga.admin.dao.impl;

import java.util.HashMap;
import java.util.Map;

import org.millioncall.openplatform.common.PaginateParams;
import org.millioncall.openplatform.common.PaginatedList;
import org.millioncall.openplatform.common.StartLimitPairImpl;
import org.millioncall.openplatform.dao.PreparedQuery;
import org.millioncall.yueyoga.admin.dao.BaseDao;
import org.millioncall.yueyoga.admin.dao.OrderDetailDAO;
import org.millioncall.yueyoga.admin.model.OrderDetail;

public class OrderDetailDAOImpl extends BaseDao implements OrderDetailDAO {

	@Override
	public OrderDetail findById(Integer id) {
		OrderDetail orderDetail = null;
		try {
			orderDetail = this.getPagableSqlSessionTemplate().selectOne(
					"findOrderDetailById", id);
		} catch (Exception e) {
			throw e;
		}
		return orderDetail;
	}

	@Override
	public PaginatedList<OrderDetail> findInPage(Integer start, Integer limit,
			String[] paramNames, String[] paramValues) {
		try {
			PreparedQuery<OrderDetail> pq = this.getPagableSqlSessionTemplate()
					.findAll("findAllOrderDetail");
			PaginatedList<OrderDetail> pl = pq.list(new PaginateParams(
					new StartLimitPairImpl(start, limit)));
			return pl;
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public PaginatedList<OrderDetail> findInPage(Integer start, Integer limit) {
		try {
			PreparedQuery<OrderDetail> pq = this.getPagableSqlSessionTemplate()
					.findAll("findAllOrderDetail");
			PaginatedList<OrderDetail> pl = pq.list(new PaginateParams(
					new StartLimitPairImpl(start, limit)));
			return pl;
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public int save(OrderDetail order) {
		int i = 0;
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("orderId", order.getOrder().getOrderId());
			map.put("productId", order.getProduct().getProductId());
			map.put("proRemainId", order.getProRemainId());
			i = this.getPagableSqlSessionTemplate().insert("saveOrderDetail",
					map);
		} catch (Exception e) {
			throw e;
		}
		return i;
	}

	@Override
	public int deleteByIds(String[] ids) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public PaginatedList<OrderDetail> findAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PaginatedList<OrderDetail> findAllOrderDetail(Integer Id,
			Integer start, Integer limit) {
		try {
			PreparedQuery<OrderDetail> pq = this.getPagableSqlSessionTemplate()
					.findAll("findOrderDetailByOrderId", Id);
			PaginatedList<OrderDetail> pl = pq.list(new PaginateParams(
					new StartLimitPairImpl(start, limit)));
			return pl;
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public int update(OrderDetail od) {
		return this.getPagableSqlSessionTemplate().update("updateOrderDetail", od);
	}

	@Override
	public PaginatedList<OrderDetail> findByOrderId(Integer start, Integer pageSize, Map<String, Object> map) {
		PreparedQuery<OrderDetail> pq = this.getPagableSqlSessionTemplate().findAll("findOrderDetailByOrderIdInPage", map);
		PaginatedList<OrderDetail> pl = pq.list(new PaginateParams(new StartLimitPairImpl(start, pageSize)));
		return pl;
	}

}
